﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.SiteMap;
using Telerik.Web.UI;
using System.Configuration;

public partial class SiteWorx_UserControls_PageEditorToolbar : System.Web.UI.UserControl
{
    public delegate void AddWebPartEventHandler(System.Web.UI.UserControl sender, EventArgs e);
    public event AddWebPartEventHandler AddWebPart;

    private string CurrentPage
    {
        get
        {
            return Page.Request.Path.ToString().Substring(Page.Request.Path.ToString().LastIndexOf("/") + 1,
                                                              Page.Request.Path.ToString().Length -
                                                              Page.Request.Path.ToString().LastIndexOf("/") - 1);

        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            SetWelcomeText();
    }

    private void SetWelcomeText()
    {
        ProfileCommon profileCommon = Profile.GetProfile(Page.User.Identity.Name);
        if (profileCommon.DisplayName.Length > 0)
            lblWelcome.Text = profileCommon.DisplayName;
        else
            lblWelcome.Text = Page.User.Identity.Name;
    }

    protected void lstPages_PreRender(object sender, EventArgs e)
    {
        if (rtsSiteWorxTabStrip.SelectedIndex == 1)
        {
            if (((RadComboBox)sender).Items.Count <= 0)
            {
                LoadMenuItems();
                LoadWebParts();
            }
        }
    }

    protected void lstPages_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (e.Value.Length > 0)
            Response.Redirect(e.Value, true);
    }

    protected void tbSiteWorxEditor_ButtonClick(object sender, RadToolBarEventArgs e)
    {
        if (e.Item.Attributes["ControlId"] != null && e.Item.Attributes["ControlId"].Length > 0)
        {
            AddWebPartEventArgs webPartEventArg = new AddWebPartEventArgs(e.Item.Attributes["ControlId"].ToString(), "~/SiteWorx/WebParts/" +
                                                                          e.Item.Attributes["ControlFilePath"].ToString(),
                                                                          e.Item.Attributes["ControlTitle"].ToString());
            AddWebPart(this, webPartEventArg);
        }
    }

    protected void tbSiteWorxEditor_Load(object sender, EventArgs e)
    {
        if (rtsSiteWorxTabStrip.SelectedIndex == 1)
        {
            rmpSiteWorxTabStrip.FindPageViewByID("pvPageEditor").Selected = true;
            tbSiteWorxEditor.Items.FindItemByValue("EditPageProperties").Enabled = ((BasePage)Page).CanEditPage;
            if (((BasePage)Page).CanEditPage)
            {
                rwmSiteWorxWindowManager.Windows[1].NavigateUrl = "~/SiteWorx/EditPage.aspx?pageId=" + Asah.SiteWorx.Site.GetSite().CurrentPage.ID;
            }

            tbSiteWorxEditor.Items.FindItemByValue("DeletePage").Enabled = ((BasePage)Page).CanDeletePage;
            hidViewPageUrl.Value = CurrentPage + "?preview=true";
            tbSiteWorxEditor.Items.FindItemByText("Web Parts").Enabled = ((BasePage)Page).CanEditPage;
        }
    }

    private void LoadMenuItems()
    {
        RadComboBox lstPages = (RadComboBox)tbtnPages.FindControl("lstPages");
        LinkedList<PageInfo> menuList = NavigationMenu.GetFullMenu();
        RadComboBoxItem listItem;

        foreach (PageInfo pageInfo in menuList)
        {
            listItem = new RadComboBoxItem();
            listItem.Text = pageInfo.MenuText;
            listItem.Value = pageInfo.Url.Replace("~/", "").ToString();
            lstPages.Items.Add(listItem);
            LoadSubMenuItems(pageInfo.ChildPages, 2);
        }

        List<PageInfo> nonMenuList = NavigationMenu.GetFullNonMenu();
        if (nonMenuList.Count > 0)
        {
            listItem = new RadComboBoxItem();
            listItem.Text = "---------Not-In-Menu---------";
            listItem.Value = "";
            lstPages.Items.Add(listItem);

            foreach (PageInfo pageInfo in nonMenuList)
            {
                listItem = new RadComboBoxItem();
                listItem.Text = pageInfo.MenuText;
                listItem.Value = pageInfo.Url.Replace("~/", "").ToString();
                lstPages.Items.Add(listItem);
            }
        }

        try
        {
            lstPages.Items.FindItemByValue(CurrentPage).Selected = true;
        }
        catch { }
    }

    private void LoadSubMenuItems(LinkedList<PageInfo> childPages, int level)
    {
        RadComboBox lstPages = (RadComboBox)tbtnPages.FindControl("lstPages");

        string prefix = "";
        for (int i = 1; i <= level - 1; i++)
        {
            prefix += Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        prefix += "|_";

        RadComboBoxItem listItem;
        foreach (PageInfo childPageInfo in childPages)
        {
            listItem = new RadComboBoxItem();
            listItem.Text = prefix + childPageInfo.MenuText;
            listItem.Value = childPageInfo.Url.Replace("~/", "").ToString();
            lstPages.Items.Add(listItem);
            LoadSubMenuItems(childPageInfo.ChildPages, level + 1);
        }
    }

    private void LoadWebParts()
    {
        RadToolBarDropDown webPartsDropDown = (RadToolBarDropDown)tbSiteWorxEditor.FindItemByText("Web Parts");
        RadToolBarButton button;
        string[] webPartConfigs;

        char[] pipeSplitter = { '|' };
        char[] commaSplitter = { ',' };

        if (ConfigurationManager.AppSettings["BasicWebParts"] != null &&
            ConfigurationManager.AppSettings["BasicWebParts"].ToString().Length > 0)
        {
            webPartConfigs = ConfigurationManager.AppSettings["BasicWebParts"].ToString().Split(pipeSplitter);
            foreach (string webPartConfig in webPartConfigs)
            {

                string[] webPartProperties = webPartConfig.Split(commaSplitter);
                button = new RadToolBarButton();
                button.Attributes.Add("ControlFilePath", webPartProperties[0].ToString());
                button.Attributes.Add("ControlId", webPartProperties[1].ToString());
                button.Attributes.Add("ControlTitle", webPartProperties[2].ToString());
                button.Text = webPartProperties[2].ToString();
                button.ImageUrl = "~/SiteWorx/WebParts/Basic/Images/" + webPartProperties[3].ToString();
                button.CausesValidation = false;
                button.PostBack = true;
                webPartsDropDown.Buttons.Add(button);
            }
        }

        if (ConfigurationManager.AppSettings["AdvancedWebParts"] != null &&
            ConfigurationManager.AppSettings["AdvancedWebParts"].ToString().Length > 0)
        {
            RadToolBarButton separatorButton = new RadToolBarButton();
            separatorButton.IsSeparator = true;
            separatorButton.Height = Unit.Pixel(1);
            webPartsDropDown.Buttons.Add(separatorButton);

            webPartConfigs = ConfigurationManager.AppSettings["AdvancedWebParts"].ToString().Split(pipeSplitter);
            foreach (string webPartConfig in webPartConfigs)
            {
                string[] webPartProperties = webPartConfig.Split(commaSplitter);

                button = new RadToolBarButton();
                button.Attributes.Add("ControlFilePath", webPartProperties[0].ToString());
                button.Attributes.Add("ControlId", webPartProperties[1].ToString());
                button.Attributes.Add("ControlTitle", webPartProperties[2].ToString());
                button.Text = webPartProperties[2].ToString();
                button.ImageUrl = "~/SiteWorx/WebParts/Advanced/Images/" + webPartProperties[3].ToString();
                button.CausesValidation = false;
                button.PostBack = true;
                webPartsDropDown.Buttons.Add(button);
            }
        }
    }

    protected void rtsSiteWorxTabStrip_Load(object sender, EventArgs e)
    {
        if (Page.ToString().StartsWith("ASP.siteworx_")) // we are on one of the SiteWorx administrative pages.
            rtsSiteWorxTabStrip.SelectedIndex = 0;
        else
            rtsSiteWorxTabStrip.SelectedIndex = 1;
    }
}